/**
 * Created by guosc on 2016/10/20 0020.
 */
define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'
],function (angular, JSONPrettyFormat) {
    var updateVariableModule = angular.module("UpdateVariableModule", [
        'DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule']);

    updateVariableModule.controller("updateVariable", function ($scope,Resource,GillionMsgService,GillionMsg) {
        $scope.variable= GillionMsgService.getInputData();
        if($scope.variable.varValueExpr.indexOf("'")==0){
            $scope.variable.varValueExpr =  $scope.variable.varValueExpr.substr(1, $scope.variable.varValueExpr.length-2)
        }
        var variables = Resource("/grule-manager/variables/"+$scope.variable.scope+"/"+$scope.variable.scopeId+"/:id",{id:"@id"});
        $scope.confirm=function(){
            if($scope.variable.valueType==0&&isNaN($scope.variable.varValueExpr)==true){
                GillionMsg.alert("提示", "请输入数字类型的表达式！");
                return;
            }else if($scope.variable.valueType==1&&$scope.variable.varValueExpr!=undefined){
                $scope.variable.varValueExpr="'"+ $scope.variable.varValueExpr+"'";
            }else if($scope.variable.valueType==2&&$scope.variable.varValueExpr!="true"&&$scope.variable.varValueExpr!="false"){
                GillionMsg.alert("提示", "请输入布尔类型的表达式！");
                return;
            }

            $scope.RuleVariableForm.verify().then(function(){
                variables.update($scope.variable,function(response){
                    if (response.success) {
                        GillionMsgService.setOutputData({success:true});
                        GillionMsgService.close();
                    } else {
                        GillionMsg.alert("提示", "变量修改失败");
                    }
                })
            })
        }
        $scope.cancel=function(){
            GillionMsgService.close();
        }

    });
    return updateVariableModule;
});